/**
 * 
 */
package info.niwota.web.apache;

import info.niwota.commons.acl.AccessControl;
import info.niwota.web.ApplicationContext;
import info.niwota.web.Debug;
import info.niwota.web.Session;
import info.niwota.web.SessionManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import x.org.apache.http.HttpEntity;
import x.org.apache.http.HttpRequest;
import x.org.apache.http.HttpResponse;
import x.org.apache.http.HttpStatus;
import x.org.apache.http.ProtocolVersion;
import x.org.apache.http.RequestLine;
import x.org.apache.http.StatusLine;
import x.org.apache.http.message.BasicHttpRequest;
import x.org.apache.http.message.BasicHttpResponse;
import x.org.apache.http.message.BasicRequestLine;
import x.org.apache.http.message.BasicStatusLine;
import x.org.apache.http.protocol.BasicHttpContext;
import x.org.apache.http.protocol.HttpContext;
import x.org.apache.http.protocol.SyncBasicHttpContext;
import android.content.Context;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class LocalAdapter extends HttpAdapter {
	private static final HttpContext webContext = new SyncBasicHttpContext(null);

	private static final ProtocolVersion protocol = new ProtocolVersion("http",
			1, 1);

	private static final String TAG = "LocalAdapter";

	public LocalAdapter(Context ctx) {
		super(ctx);

	}

	private HttpContext createContext() {
		String jid = ApplicationContext.getJid();
		
		HttpContext c = new BasicHttpContext(null);
		c.setAttribute(SessionManager.WEB_CONTEXT_KEY, webContext);
		c.setAttribute(SessionManager.WEB_JID_KEY, jid);
		c.removeAttribute(SessionManager.WEB_PID_KEY);
	
		return c;
	}
	
	/**
	 * 
	 */
	public void handle(String user, String uri, OutputStream os) throws Exception {
		RequestLine requestLine = new BasicRequestLine("GET", uri, protocol);
		HttpRequest req = new BasicHttpRequest(requestLine);
		StatusLine statusLine = new BasicStatusLine(protocol, HttpStatus.SC_OK,
				null);
		HttpResponse res = new BasicHttpResponse(statusLine);
		
		HttpContext context = createContext();
		
		fakeLogin(user, context);
		
		handle(req, res, context);
		
		//
		HttpEntity entity = res.getEntity();
		entity.writeTo(os);
	}
	
	private void fakeLogin(String user, HttpContext context) throws IOException {
		Session s = (Session) context.getAttribute( SessionManager.SESSION_KEY);
		if (s == null) {
			s = SessionManager.create();
		}
		
		//
		s.user = user;
		s.group = AccessControl.ACCESS_MASTER;
		s.authenticated = true;
		
		context.setAttribute( SessionManager.SESSION_KEY, s);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "user " + s.user + " fake login " + s.authenticated + " as group: " + s.group);
		}
	}

	public void handle(String user, String contentUri, File file) throws Exception {
		FileOutputStream os = new FileOutputStream(file);
		try {
			handle(user, contentUri, os);
		} finally {
			os.close();
		}
	}
}
